package com.jc.subject.domain.handler;

import com.jc.subject.common.enums.IsDeletedFlagEnum;
import com.jc.subject.common.enums.SubjectTypeFlagEnum;
import com.jc.subject.domain.convert.SubjectAnswerConverter;
import com.jc.subject.domain.convert.SubjectHandlerConverter;
import com.jc.subject.domain.entity.SubjectAnswerDO;
import com.jc.subject.domain.entity.SubjectInfoDO;
import com.jc.subject.domain.entity.SubjectOptionDO;
import com.jc.subject.infra.basic.entity.SubjectBrief;
import com.jc.subject.infra.basic.entity.SubjectRadio;
import com.jc.subject.infra.basic.service.SubjectBriefService;
import com.jc.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author gongjiawei
 * @Date 2024/7/9 14:32
 * @注释
 */
@Component
public class SubjectRadioHandler implements SubjectStrategy{


    @Resource
    private SubjectRadioService subjectRadioService;
    @Override
    public SubjectTypeFlagEnum getcode() {
        return SubjectTypeFlagEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoDO subjectInfoDO) {

        //将信息插入单选表中 TODO
        List<SubjectAnswerDO> optionList = subjectInfoDO.getOptionList();
        if (optionList.isEmpty())return;
        List<SubjectRadio> subjectRadios = SubjectAnswerConverter.INSTANCE.convertBoToRadioList(optionList);
        subjectRadios.forEach(subjectRadio -> {
            subjectRadio.setSubjectId(subjectInfoDO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        });
        subjectRadioService.batchInsert(subjectRadios);



    }

    @Override
    public SubjectOptionDO query(int subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId((long) subjectId);
        List<SubjectRadio> result = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerDO> subjectAnswerBOList = SubjectHandlerConverter.INSTANCE.convertRadioEntityToBoList(result);
        SubjectOptionDO subjectOptionBO = new SubjectOptionDO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
